namespace Stee.CAP8.PropertyChangedNotificator
{
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Linq.Expressions;
    using Stee.CAP8.PropertyChangedNotificator.Attributes;

    public abstract class NotificationObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void AddPropertyChangedEventHandler(PropertyChangedEventHandler @eventHandler)
        {
            PropertyChanged -= @eventHandler;
            PropertyChanged += @eventHandler;

        }

        protected void RemovePropertyChangedEventHandler() => this.PropertyChanged = null;



        [DebuggerStepThrough]
        [NotificationInvocator]
        protected void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        [DebuggerStepThrough]
        protected void RaisePropertyChanged<T>(Expression<Func<T>> expression)
        {
            var name = ((MemberExpression)expression.Body).Member.Name;
            RaisePropertyChanged(name);
        }
    }
}
